<?php
namespace app\admin\controller;

/**
 * Class System
 * @package app\admin\controller
 * @property \app\admin\service\System $service
 */
class System extends Admin
{

	protected $noNeedLogin = [];
	protected $noNeedAuth = ['*'];

	public function index()
	{
		$tmp = \app\admin\model\Config::where('key','config_group')->where('group','base')->value('option');
		$array = explode("\n",$tmp);
		$group = [];
		$config = config('base.config_group')??config('app.config_group');

		$group = array_map(function($item)use($config){
			$op = explode(':',$item);
			if(!in_array($op[0],$config)){
				return ;
			}
			return  [
				'tag' => $op[0],
				'name' => $op[1]
			];
		},$array);
		$group = array_filter($group);
		$this->assign('flag',$this->request->get('group','base'));
		$this->assign('html', $this->service->getConfigHtml());
		$this->assign('group',$group);
		return $this->fetch();
	}
	
	public function create()
	{
		if ( $this->request->isPost() ){
			return $this->service->create();
		}
		return $this->fetch();
	}
	
	public function update($id=null)
	{
		if ( $this->request->isPost() ){
			$this->service->update();
		}
		
		$info = $this->service->getModel()->find($id);
		$this->assign('info', $info);
		return $this->fetch();
	}
	
	public function delete(){
		return $this->service->delete();
	}
	
}
